extends Control

func _ready() -> void:
	var confirm_btn: Button = $CenterContainer/VBoxContainer/btns/confirm
	var cancel_btn: Button = $CenterContainer/VBoxContainer/btns/cancel
	
	# 复制信息到设置界面
	Global.tmp_global_config = Global.global_config.deep_copy()
	Global.tmp_sun_1_config = Global.sun_1_config.deep_copy()
	Global.tmp_sun_2_config = Global.sun_2_config.deep_copy()
	Global.tmp_sun_3_config = Global.sun_3_config.deep_copy()
	
	confirm_btn.pressed.connect(confirm_profile)
	cancel_btn.pressed.connect(cancel_profile)
	
	# 实时更新控件值到临时变量
	# 星球1
	# 位置
	var loc_edit_x_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/loc_edit/loc_edit_x
	var loc_edit_y_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/loc_edit/loc_edit_y
	var loc_edit_z_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/loc_edit/loc_edit_z
	
	loc_edit_x_1.text = str(Global.tmp_sun_1_config.loc.x)
	loc_edit_y_1.text = str(Global.tmp_sun_1_config.loc.y)
	loc_edit_z_1.text = str(Global.tmp_sun_1_config.loc.z)
	loc_edit_x_1.text_changed.connect(func(s):Global.tmp_sun_1_config.loc = Vector3(_limit_as_num(s, loc_edit_x_1), Global.tmp_sun_1_config.loc.y, Global.tmp_sun_1_config.loc.z))
	loc_edit_y_1.text_changed.connect(func(s):Global.tmp_sun_1_config.loc = Vector3(Global.tmp_sun_1_config.loc.x, _limit_as_num(s, loc_edit_y_1), Global.tmp_sun_1_config.loc.z))
	loc_edit_z_1.text_changed.connect(func(s):Global.tmp_sun_1_config.loc = Vector3(Global.tmp_sun_1_config.loc.x, Global.tmp_sun_1_config.loc.y, _limit_as_num(s, loc_edit_z_1)))
	# 质量
	var mass_edit_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/mass_edit

	mass_edit_1.text = str(Global.tmp_sun_1_config.mass)
	mass_edit_1.text_changed.connect(func(s):Global.tmp_sun_1_config.mass = _limit_as_num(s, mass_edit_1))
	# 初始速度
	var vel_edit_x_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/vel_edit/vel_edit_x
	var vel_edit_y_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/vel_edit/vel_edit_y
	var vel_edit_z_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/vel_edit/vel_edit_z
	
	vel_edit_x_1.text = str(Global.tmp_sun_1_config.vel.x)
	vel_edit_y_1.text = str(Global.tmp_sun_1_config.vel.y)
	vel_edit_z_1.text = str(Global.tmp_sun_1_config.vel.z)
	vel_edit_x_1.text_changed.connect(func(s):Global.tmp_sun_1_config.vel = Vector3(_limit_as_num(s, vel_edit_x_1), Global.tmp_sun_1_config.vel.y, Global.tmp_sun_1_config.vel.z))
	vel_edit_y_1.text_changed.connect(func(s):Global.tmp_sun_1_config.vel = Vector3(Global.tmp_sun_1_config.vel.x, _limit_as_num(s, vel_edit_y_1), Global.tmp_sun_1_config.vel.z))
	vel_edit_z_1.text_changed.connect(func(s):Global.tmp_sun_1_config.vel = Vector3(Global.tmp_sun_1_config.vel.x, Global.tmp_sun_1_config.vel.y, _limit_as_num(s, vel_edit_z_1)))
	# 直径
	var diam_edit_1: LineEdit = $CenterContainer/VBoxContainer/sun1_profile/diam_edit

	diam_edit_1.text = str(Global.tmp_sun_1_config.diam)
	diam_edit_1.text_changed.connect(func(s):Global.tmp_sun_1_config.diam = _limit_as_num(s, diam_edit_1))
	
	# 星球2
	# 位置
	var loc_edit_x_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/loc_edit/loc_edit_x
	var loc_edit_y_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/loc_edit/loc_edit_y
	var loc_edit_z_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/loc_edit/loc_edit_z

	loc_edit_x_2.text = str(Global.tmp_sun_2_config.loc.x)
	loc_edit_y_2.text = str(Global.tmp_sun_2_config.loc.y)
	loc_edit_z_2.text = str(Global.tmp_sun_2_config.loc.z)
	loc_edit_x_2.text_changed.connect(func(s):Global.tmp_sun_2_config.loc = Vector3(_limit_as_num(s, loc_edit_x_2), Global.tmp_sun_2_config.loc.y, Global.tmp_sun_2_config.loc.z))
	loc_edit_y_2.text_changed.connect(func(s):Global.tmp_sun_2_config.loc = Vector3(Global.tmp_sun_2_config.loc.x, _limit_as_num(s, loc_edit_y_2), Global.tmp_sun_2_config.loc.z))
	loc_edit_z_2.text_changed.connect(func(s):Global.tmp_sun_2_config.loc = Vector3(Global.tmp_sun_2_config.loc.x, Global.tmp_sun_2_config.loc.y, _limit_as_num(s, loc_edit_z_2)))
	# 质量
	var mass_edit_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/mass_edit

	mass_edit_2.text = str(Global.tmp_sun_2_config.mass)
	mass_edit_2.text_changed.connect(func(s):Global.tmp_sun_2_config.mass = _limit_as_num(s, mass_edit_2))
	# 初始速度
	var vel_edit_x_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/vel_edit/vel_edit_x
	var vel_edit_y_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/vel_edit/vel_edit_y
	var vel_edit_z_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/vel_edit/vel_edit_z
	
	vel_edit_x_2.text = str(Global.tmp_sun_2_config.vel.x)
	vel_edit_y_2.text = str(Global.tmp_sun_2_config.vel.y)
	vel_edit_z_2.text = str(Global.tmp_sun_2_config.vel.z)
	vel_edit_x_2.text_changed.connect(func(s):Global.tmp_sun_2_config.vel = Vector3(_limit_as_num(s, vel_edit_x_2), Global.tmp_sun_2_config.vel.y, Global.tmp_sun_2_config.vel.z))
	vel_edit_y_2.text_changed.connect(func(s):Global.tmp_sun_2_config.vel = Vector3(Global.tmp_sun_2_config.vel.x, _limit_as_num(s, vel_edit_y_2), Global.tmp_sun_2_config.vel.z))
	vel_edit_z_2.text_changed.connect(func(s):Global.tmp_sun_2_config.vel = Vector3(Global.tmp_sun_2_config.vel.x, Global.tmp_sun_2_config.vel.y, _limit_as_num(s, vel_edit_z_2)))
	# 直径
	var diam_edit_2: LineEdit = $CenterContainer/VBoxContainer/sun2_profile/diam_edit

	diam_edit_2.text = str(Global.tmp_sun_2_config.diam)
	diam_edit_2.text_changed.connect(func(s):Global.tmp_sun_2_config.diam = _limit_as_num(s, diam_edit_2))
	
	# 星球3
	# 位置
	var loc_edit_x_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/loc_edit/loc_edit_x
	var loc_edit_y_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/loc_edit/loc_edit_y
	var loc_edit_z_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/loc_edit/loc_edit_z

	loc_edit_x_3.text = str(Global.tmp_sun_3_config.loc.x)
	loc_edit_y_3.text = str(Global.tmp_sun_3_config.loc.y)
	loc_edit_z_3.text = str(Global.tmp_sun_3_config.loc.z)
	loc_edit_x_3.text_changed.connect(func(s):Global.tmp_sun_3_config.loc = Vector3(_limit_as_num(s, loc_edit_x_3), Global.tmp_sun_3_config.loc.y, Global.tmp_sun_3_config.loc.z))
	loc_edit_y_3.text_changed.connect(func(s):Global.tmp_sun_3_config.loc = Vector3(Global.tmp_sun_3_config.loc.x, _limit_as_num(s, loc_edit_y_3), Global.tmp_sun_3_config.loc.z))
	loc_edit_z_3.text_changed.connect(func(s):Global.tmp_sun_3_config.loc = Vector3(Global.tmp_sun_3_config.loc.x, Global.tmp_sun_3_config.loc.y, _limit_as_num(s, loc_edit_z_3)))
	# 质量
	var mass_edit_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/mass_edit

	mass_edit_3.text = str(Global.tmp_sun_3_config.mass)
	mass_edit_3.text_changed.connect(func(s):Global.tmp_sun_3_config.mass = _limit_as_num(s, mass_edit_3))
	# 初始速度
	var vel_edit_x_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/vel_edit/vel_edit_x
	var vel_edit_y_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/vel_edit/vel_edit_y
	var vel_edit_z_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/vel_edit/vel_edit_z
	
	vel_edit_x_3.text = str(Global.tmp_sun_3_config.vel.x)
	vel_edit_y_3.text = str(Global.tmp_sun_3_config.vel.y)
	vel_edit_z_3.text = str(Global.tmp_sun_3_config.vel.z)
	vel_edit_x_3.text_changed.connect(func(s):Global.tmp_sun_3_config.vel = Vector3(_limit_as_num(s, vel_edit_x_3), Global.tmp_sun_3_config.vel.y, Global.tmp_sun_3_config.vel.z))
	vel_edit_y_3.text_changed.connect(func(s):Global.tmp_sun_3_config.vel = Vector3(Global.tmp_sun_3_config.vel.x, _limit_as_num(s, vel_edit_y_3), Global.tmp_sun_3_config.vel.z))
	vel_edit_z_3.text_changed.connect(func(s):Global.tmp_sun_3_config.vel = Vector3(Global.tmp_sun_3_config.vel.x, Global.tmp_sun_3_config.vel.y, _limit_as_num(s, vel_edit_z_3)))
	# 直径
	var diam_edit_3: LineEdit = $CenterContainer/VBoxContainer/sun3_profile/diam_edit

	diam_edit_3.text = str(Global.tmp_sun_3_config.diam)
	diam_edit_3.text_changed.connect(func(s):Global.tmp_sun_3_config.diam = _limit_as_num(s, diam_edit_3))
	
	# 全局设置
	# 引力常数
	var const_g_edit: LineEdit = $CenterContainer/VBoxContainer/global_profile/const_g_edit
	const_g_edit.text = str(Global.tmp_global_config.const_g)
	const_g_edit.text_changed.connect(func(s):Global.tmp_global_config.const_g = _limit_as_num(s, const_g_edit))
	# max_fps
	var max_fps_edit: LineEdit = $CenterContainer/VBoxContainer/global_profile/fps_edit
	max_fps_edit.text = str(Global.tmp_global_config.fps)
	max_fps_edit.text_changed.connect(func(s):Global.tmp_global_config.fps = _limit_as_num(s, max_fps_edit))
	# 鼠标灵敏度
	var sens: LineEdit = $CenterContainer/VBoxContainer/global_profile/sens_edit
	sens.text = str(Global.tmp_global_config.sensitive)
	sens.text_changed.connect(func(s):Global.tmp_global_config.sensitive = _limit_as_num(s, sens))
	
enum TYP {
		INT,
		FLOAT,
		VEC3X,
		VEC3Y,
		VEC3Z
	}

func _limit_as_num(s: String, node: LineEdit) -> float:
	# 将输入限制为数字 不会把整型转为浮点型
	# 允许负数和科学计数法
	var have_point: bool = false
	var have_e: bool = false
	var last_s: String
	var have_nag: bool = false
	var cursor: int = node.caret_column
	var new_s: String = ""
	var c_index: int = 0
	var new_cursor: int = 0
	for i in s:
		
		Global.log.debug("last_str: %s"%last_s)
		#if i.is_valid_int() or (i == "." and not have_point):
			#if i == ".":
				#have_point = true
			#new_s += i
			#new_cursor += 1
		if i.is_valid_int() or (i == "." and not have_point) or (i.to_lower() == "e" and not have_e and c_index != 0) or (i == "-" and not have_nag and (c_index == 0 or last_s.to_lower() == "e")):
			if i == ".":
				have_point = true
			elif i.to_lower() == "e":
				have_e = true
			elif i == "-":
				have_nag = true
			new_s += i
			new_cursor += 1
			
		if c_index >= cursor:
			new_cursor -= 1
		c_index += 1
		
		last_s = i
	node.text = new_s
	node.caret_column = new_cursor
	return float(new_s)

func cancel_profile():
	Global.menu_box.get_parent().remove_child(Global.menu_box)
	Global.menu_box.queue_free()
	Global.menu_box = null

func confirm_profile():
	# 找到所有设置
	# 删除游戏内星球
	# 根据设置重置星球
	# 保存为用户配置
	# 删除设置节点
	if not Global.sunbox:
		Global.log.error("未初始化Suns类")
		return
	Global.log.info("用户确定配置 重置星球")
	Global.log.info("删除旧星球")
	Global.sunbox.del_suns()
	Global.log.info("修改全局设置")
	Global.sunbox.const_g = Global.tmp_global_config.const_g
	Global.log.info("锁定fps: %d"%Global.global_config.fps)
	Engine.max_fps = Global.tmp_global_config.fps
	
	Global.log.info("添加新星球")
	Global.sunbox.add_sun(
		Global.tmp_sun_1_config.mass, 
		Global.tmp_sun_1_config.diam, 
		Global.tmp_sun_1_config.loc, Global.sun_1_config.vel)
		
	Global.sunbox.add_sun(
		Global.tmp_sun_2_config.mass, 
		Global.tmp_sun_2_config.diam, 
		Global.tmp_sun_2_config.loc, Global.sun_2_config.vel)
	
	Global.sunbox.add_sun(
		Global.tmp_sun_3_config.mass, 
		Global.tmp_sun_3_config.diam, 
		Global.tmp_sun_3_config.loc, Global.sun_3_config.vel)
		
	Global.log.info("覆盖配置")
	Global.global_config = Global.tmp_global_config
	Global.sun_1_config = Global.tmp_sun_1_config
	Global.sun_2_config = Global.tmp_sun_2_config
	Global.sun_3_config = Global.tmp_sun_3_config
	
	Global.log.info("持久化配置到用户配置文件")
	var dict: Dictionary = {
		"global": {
			"const_g": Global.tmp_global_config.const_g,
			"cal_freq": 60,
			"fps": Global.tmp_global_config.fps,
			"sensitive": 0.001,
			"tail": 0.
		},
		"suns": [
			{
				"mass": Global.tmp_sun_1_config.mass,
				"diam": Global.tmp_sun_1_config.diam,
				"loc": Global._vec3_to_a(Global.tmp_sun_1_config.loc),
				"vel": Global._vec3_to_a(Global.sun_1_config.vel),
				"acc": [0., 0., 0.],
			},
			{
				"mass": Global.tmp_sun_2_config.mass,
				"diam": Global.tmp_sun_2_config.diam,
				"loc": Global._vec3_to_a(Global.tmp_sun_1_config.loc),
				"vel": Global._vec3_to_a(Global.sun_1_config.vel),
				"acc": [0., 0., 0.],
			},
			{
				"mass": Global.tmp_sun_3_config.mass,
				"diam": Global.tmp_sun_3_config.diam,
				"loc": Global._vec3_to_a(Global.tmp_sun_1_config.loc),
				"vel": Global._vec3_to_a(Global.sun_1_config.vel),
				"acc": [0., 0., 0.],
			}
		]
	}
	Global._set_user_profile(dict)
	
	cancel_profile()
